package com.forlove.app.qselect.friend.widget;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import com.forlove.app.R;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.widget.recyview.BaseRecyclerAdapter;
import com.forlove.app.common.widget.recyview.ViewHolder;
import com.forlove.app.qselect.friend.entity.SelectEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yjl on 2017/11/27.
 */

public class SelectTermView extends ConstraintLayout {
    private View mRootView;
    private RecyclerView recyclerview_choice_term;
    private View bg_choice_term;
    private List<SelectEntity> termNames = new ArrayList<>();
    private TermAdapter termAdapter;
    private Context mContext;
    private SelectEntity selectEntity;
    private SelectTermListener selectTermListener;

    public SelectTermView(Context context) {
        super(context);
        initView(context);
    }

    public SelectTermView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public SelectTermView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        this.mContext = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mRootView = inflater.inflate(R.layout.layout_select_view, this);
        bg_choice_term = mRootView.findViewById(R.id.bg_choice_term);
        bg_choice_term.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setVisibility(GONE);
            }
        });
        recyclerview_choice_term = mRootView.findViewById(R.id.recyclerview_choice_term);
        termAdapter = new TermAdapter(mContext, R.layout.item_select_term, termNames);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recyclerview_choice_term.setAdapter(termAdapter);
        recyclerview_choice_term.setLayoutManager(linearLayoutManager);
        termAdapter.hideEmptyView();
        termAdapter.hideFooterView();
        termAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                LogUtil.e("yjl", "selectTermView onItemClick");
                for (int i = 0; i < termNames.size(); i++) {
                    //单选重制状态
                    SelectEntity selectEntity = termNames.get(i);
                    if (selectEntity.isSelect) {
                        selectEntity.isSelect = false;
                        termAdapter.notifyItemChanged(i);
                        break;
                    }
                }
                selectEntity = termNames.get(position);
                selectEntity.selectPosition = position;
                selectEntity.isSelect = true;
                if (selectTermListener != null) {
                    selectTermListener.selectTerm(selectEntity);
                }
                termAdapter.notifyItemChanged(position);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    public void updateTermName(List<SelectEntity> selectEntities, int select) {
        if (selectEntities != null) {
            termNames.clear();
            termNames.addAll(selectEntities);
            if (select > 0 && select < termNames.size()) {
                termNames.get(select).isSelect = true;
            }
            termAdapter.updateDatas(selectEntities);
        }
    }

    private class TermAdapter extends BaseRecyclerAdapter<SelectEntity> {
        public TermAdapter(Context context, int itemView, List<SelectEntity> mDatas) {
            super(context, itemView, mDatas);
        }

        @Override
        protected void convertView(ViewHolder holder, SelectEntity entity, int position) {
            holder.setText(R.id.text_term_name, entity.selectName);
            if (entity.isSelect) {
                holder.setTextColor(R.id.text_term_name, getResources().getColor(R.color.common_green));
            } else {
                holder.setTextColor(R.id.text_term_name, getResources().getColor(R.color.str_color_gray));
            }
        }
    }

    @Override
    public void setVisibility(int visibility) {
        if (visibility == VISIBLE && !isShown()) {
            showBackground();
        } else if (visibility == GONE || visibility == INVISIBLE) {
            hideBackGround();
        }
        super.setVisibility(visibility);
    }

    private void showBackground() {
        Animator animation =
                AnimatorInflater.loadAnimator(mContext, R.animator.anim_aphla_show);
        animation.setTarget(bg_choice_term);
        animation.start();
        animation.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
//                if (!isShown()) {
//                    setVisibility(VISIBLE);
//                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    private void hideBackGround() {
        Animator animation =
                AnimatorInflater.loadAnimator(mContext, R.animator.anim_aphla_show);
        animation.setTarget(bg_choice_term);
        animation.start();
        animation.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
//                if (isShown()) {
//                    setVisibility(GONE);
//                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }


    /**
     * 获取选中的内容
     *
     * @return
     */
    public SelectEntity getSelectEntity() {
        return selectEntity;
    }

    public void setSelectTermListener(SelectTermListener selectTermListener) {
        this.selectTermListener = selectTermListener;
    }


    public interface SelectTermListener {
        void selectTerm(SelectEntity selectEntity);
    }
}
